<?php

namespace App\Services;

use App\CodeResponse;
use App\Models\Goods;
use App\Models\GoodsBrand;
use App\Models\GoodsCart;
use App\Models\GoodsCategory;
use App\Models\GoodsSku;
use App\Models\GoodsSpecs;
use App\Models\GoodsSpecsValue;

class GoodsService extends BaseService
{
    /**
     * 获取列表
     * @return
     */
    public function list($page,$limit,$sort,$order,$goods_brand_id,$goods_category_id,$title,$status) {
        $query = Goods::leftJoin('goods_brands', 'goods_brands.id', '=', 'goods.goods_brand_id')
            ->leftJoin('goods_categories', 'goods_categories.id', '=', 'goods.goods_category_id')
            ->leftJoin('goods_skus', 'goods_skus.id', '=', 'goods.sku_id')
            ->select('goods.*',
                'price','goods_specs_value_id','cost_price','goods_skus.stock as usStock ',
                'goods_brands.name as brandsName','goods_categories.name as categoriesName'
            );
        if(isset($goods_brand_id)){
            $query->where('goods_brand_id',$goods_brand_id);
        }
        if(isset($goods_category_id)){
            $query->whereRaw('FIND_IN_SET(?,goods_category_path_ids)',[$goods_category_id]);
        }
        if(!empty($title)){
            $query->where('goods.title','like',"%$title%");
        }
        if(isset($status)){
            $query->where('goods.status',$status);
        }
        $query->where('goods.status','!=',2);
        return $query->orderBy($sort,$order)->paginate($limit, ['*'], 'page', $page);
    }

    /**
     * 获取修改信息
     * @return
     */
    public function updateInfo($goodsId) {
        $goods = Goods::where('id',$goodsId)->first();
        $goods->brandName = '';
        $goods->categoryName ='' ;
        $goods->categoryArrName= [];
        $goods->specs = [];
        if($goods){
            $brand = GoodsBrand::where('id',$goods->goods_brand_id)->select('id', 'name')->first();
            $categoryIdArr = explode(',',$goods->goods_category_path_ids);
            $categoryArr = GoodsCategory::whereIn('id',$categoryIdArr)->select('id', 'name')->get();
            $goods->brandName = $brand ? $brand->name : '';
            $category = [];
            foreach ($categoryArr as $v){
                $goods->categoryName ='' ;
                array_push($category,$v);
            }
            $goods->categoryArrName= $category;
            $goods->categoryName =count($category)>0 ? $category[count($category)-1]->name : '';
            $specsIdArr = explode(',',$goods->goods_specs_ids);
            //获取规格
            $specsArr = GoodsSpecs::whereIn('id',$specsIdArr)->select('id', 'name')->get();
            $specsValArr = GoodsSpecsValue::whereIn('goods_specs_id',$specsIdArr)->select('goods_specs_id','id', 'name')->get();
            foreach ($specsArr as $item) {
                $arr = [];
                foreach ($specsValArr as $item2) {
                    if ($item->id == $item2->goods_specs_id){
                        array_push( $arr,$item2);
                    }
                }
                $item->specsVal = $arr;
            }
            $goods->specs = $specsArr;
            $sku = GoodsSku::where('goods_id',$goods->id)->get();
            $goods->sku = $sku;

        }
        return $goods;



    }
    /**
     * 减少库存数量
     * @return
     */
    public function reduceSkuNum($skuId,$num) {
        $goodsSku = GoodsSku::find($skuId);
        $goodsSku->stock = ($goodsSku->stock-$num);
        $goodsSku->save();

        //更新库存
        $sum= GoodsSku::where('goods_id', $goodsSku->goods_id)->sum('stock');
        $goodsModel = Goods::find($goodsSku->goods_id);
        $goodsModel->stock = $sum;
        $goodsModel->save();
    }
    /**
     * 获取商品信息
     * @return
     */
    public function getSkuInfo($skuId) {
        $goodsSku = GoodsSku::where('id',$skuId)->first();
        if($goodsSku){
            $goods = Goods::where('id',$goodsSku->goods_id)->first();
            //获取规格
            $specsIdArr = explode(',',$goods->goods_specs_ids);
            $specsArr = GoodsSpecs::whereIn('id',$specsIdArr)->select('id', 'name')->get();
            $specsvalueIdArr = explode(',',$goodsSku->goods_specs_value_id);
            $specsValArr = GoodsSpecsValue::whereIn('id',$specsvalueIdArr)->select('goods_specs_id','id', 'name')->get();
            foreach ($specsArr as $item) {
                $arr = [];
                foreach ($specsValArr as $item2) {
                    if ($item->id == $item2->goods_specs_id){
                        array_push( $arr,$item2);
                    }
                }
                $item->specsVal = $arr;
            }
            $goodsSku->title = $goods->title; //名称
            $goodsSku->sub_title = $goods->sub_title; //副标题
            $goodsSku->goods_num = $goods->goods_num; //商品编号
            $goodsSku->unit = $goods->unit; //单位
            $goodsSku->goods_specs_ids = $goods->goods_specs_ids; //规格id
//            $goodsSku->sku_id = $goods->sku_id; //商品默认的skuid
            $goodsSku->is_show_stock = $goods->is_show_stock; //是否显示库存
//            $goodsSku->stock = $goods->stock; //库存
            $goodsSku->production_time = $goods->production_time; //生产日期

            $goodsSku->image = $goods->image; //默认图
            $goodsSku->carousel_image = $goods->carousel_image; //商品轮播图
            $goodsSku->description = $goods->description; //商品详情
            $goodsSku->status = $goods->status; //状态
            $goodsSku->recommend = $goods->recommend; //是否推荐
            $goodsSku->specs = $specsArr;
        }
        return $goodsSku;
    }

}
